require "Common/define"
require "Logic/Config"
require "Common/functions"
---@class HeadRectBItem
HeadRectBItem = {}
HelperFunc.SetUIEnv(HeadRectBItem)
local this = HeadRectBItem;
this.linkColor = "#"..G.dataTable["t_global"][tostring(185)]["f_string_value"];
this.mainColor = "#"..G.dataTable["t_global"][tostring(237)]["f_string_value"];
this.gameObject = nil;
this.transform = nil;
this.LuaBehaviour = nil;
this.imgType = nil;
this.imgQualityBg = nil;
this.imgQualityFrame = nil;
this.gemstoneController = nil;
this.imgIcon = nil;
this.txtChipCountMax = nil;
this.needText = nil;
this.ownText = nil;
--this.txtName = nil;
this.txtLv = nil;
this.ownCount = nil;
this.showOwnCount = nil;
---@type FairyGUI.Controller
this.objUnActive = nil;
this.objUnActiveLockGroup = nil;
this.objUnActiveSummonGroup = nil;
this.imgChipSilder = nil;
this.txtChipCount = nil;
this.objChip = nil;
this.typeCtrl = nil

local qualityToBgName = { "hero_card_face_gre", "hero_card_face_blue", "hero_card_face_vio","hero_card_face_yel", "hero_card_face_red", "hero_card_face_0" };
local godQualityToBgName = {
    [1] = qualityToBgName[1],
    [2] = qualityToBgName[2],
    [3] = qualityToBgName[3],
    [4] = "hero_card_face_yel_shen",
    [5] = "hero_card_face_red_shen",
    [6] = "hero_card_face_0_shen"}
local demonQualityToBgName = {
    [1] = qualityToBgName[1],
    [2] = qualityToBgName[2],
    [3] = qualityToBgName[3],
    [4] = "hero_card_face_yel_mo",
    [5] = "hero_card_face_red_mo",
    [6] = "hero_card_face_0_mo"}
local roseQualityToBgName = {
    [1] = qualityToBgName[1],
    [2] = qualityToBgName[2],
    [3] = qualityToBgName[3],
    [4] = "hero_card_face_yel_ss",
    [5] = "hero_card_face_red_ss",
    [6] = "hero_card_face_0_ss"}
function HeadRectBItem.Init()
end

function HeadRectBItem.onCreate(obj)
    obj:SetActive(false);
end
---@param obj FairyGUI.GComponent
function HeadRectBItem.initData(obj, data,type,extraInfo,iswish, isHeroSwap)
    gemstoneController = obj:GetController("break");
    imgQualityFrame = obj:GetChild("quality")
    imgQualityBg = obj:GetChild("qualityBg")
    imgQualityMask = obj:GetChild("mask")
    imgIcon = obj:GetChild("Icon")
    imgIcon = imgIcon:GetChild("icon")
    txtLv = obj:GetChild("level")
    imgType = obj:GetChild("type")
    ownCount = obj:GetChild("ownCount")
    needText = obj:GetChild("need");
    ownText = obj:GetChild("own");
    this.new_effect = obj:GetChild("new_effect");

    objUnActive = obj:GetController("ctrlUnActive");
    objUnActiveSummonGroup = obj:GetController("ctrlSummon")
    showOwnCount = obj:GetController("showOwnCount")
    if obj:GetController("New") then
        this.isNewController = obj:GetController("New");
        this.isNewController.selectedIndex = 0;
    else
        this.isNewController = nil;
    end
    txtChipCount = obj:GetChild("own");
    txtChipCountMax = obj:GetChild("need");
    imgIcon.visible = false
    txtLv.visible = false
    imgType.visible = false
    objUnActive.selectedIndex = 1
    showOwnCount.selectedIndex = 1
    typeCtrl = obj:GetController("topicon")
    this.activationCtrl=obj:GetController("activation")
    if this.activationCtrl then
        this.activationCtrl.selectedIndex=0
    end
    if data then
        if typeCtrl and data["faction"] then
            typeCtrl.selectedIndex = data["faction"]
        end
        if data.partnerId ~= 0 and not data.notShowNum then
            local hero = logicMgr.HeroManager.getHeroDatabyPartnerId(tostring(data.partnerId),true);
            local curSerchID = hero["partnerModelId"] * 1000 + hero.star;
            local config = G.dataTable["t_herorank"][tostring(curSerchID)]
            local sameNum = ItemManager.getItemCountbyModelId(hero.storeId)
            if config == nil then
                logError("找不到英雄等级t_herorank f_HeroRankID:"..tostring(curSerchID));
            end
            if config == nil then
                --ownText.text = "-";
                --needText.text = "-";
                showOwnCount.selectedIndex = 0

            else
                if sameNum == nil then
                    --ownText.text = "-";
                    --needText.text = "-"
                    showOwnCount.selectedIndex = 0

                else
                    if sameNum < config.f_RankUpCost then
                        ownText.text = sameNum;
                    else
                        ownText.text = "[color=#00FF00]" .. sameNum .. "[/color]";
                    end
                    needText.text = config.f_RankUpCost;
                end
            end
        else
            showOwnCount.selectedIndex = 0
            if extraInfo~=nil then
                if extraInfo.isHeroMain and not extraInfo.hasPossess then
                    showOwnCount.selectedIndex = 1
                    local hero = logicMgr.HeroManager.getHeroDatabyModelId(tostring(data.partnerModelId),true)
                    local ownNum = ItemManager.getItemCountbyModelId(hero.storeId)
                    local needNum=G.dataTable["t_hero"][tostring(data.partnerModelId)].f_ActiveCount
                    if ownNum<needNum then
                        ownText.text=tostring(ownNum)
                        extraInfo.canActive=false
                    else
                        ownText.text = "[color=#00FF00]" .. ownNum .. "[/color]"
                        if this.activationCtrl then
                            this.activationCtrl.selectedIndex=1
                        end
                        extraInfo.canActive=true
                    end
                    needText.text = needNum
                end
            end
        end
    end
    if data ~= nil then
        if data["halfIcon"] ~= nil then
            imgIcon.visible = true
            if data.skinId and tonumber(data.skinId) > 0 then
                imgIcon.url = "ui://HalfBodyAtlas/" ..logicMgr.SkinManager.GetHeroBIconBySkinId(data.skinId)
            else
                imgIcon.url = "ui://HalfBodyAtlas/" .. data["halfIcon"]
            end
        end
        if data["star"] ~= nil then
            local bgName = qualityToBgName
            if data.faction == 1 then
                bgName = godQualityToBgName
            elseif data.faction == 2 then
                bgName = demonQualityToBgName
            elseif data.faction == 3 then
                bgName = roseQualityToBgName
            end
            local quality = tonumber(data["star"]) / 10;
            quality = math.floor(quality)
            if quality == 1 then
                imgQualityBg.url = "ui://ICONAtlas/" .. bgName[1];
            else
                if math.floor(quality / 2) + 1 >= 6 then
                    imgQualityBg.url = "ui://ICONAtlas/" .. bgName[6];
                else
                    imgQualityBg.url = "ui://ICONAtlas/" .. bgName[math.floor(quality / 2) + 1];
                end
                if math.fmod(quality, 2) == 1 or quality > 10 then
                    --如果为单数都有宝石
                    if quality > 10 then
                        --钻石品质
                        local lv = quality - 9;
                        gemstoneController.selectedIndex = lv >= 6 and 6 or lv;
                    else
                        gemstoneController.selectedIndex = 1;
                    end
                else
                    gemstoneController.selectedIndex = 0;
                end
            end
            if imgQualityMask then
                imgQualityMask.url = imgQualityBg.url;
            end
        end

        

        if data["type"] ~= nil then
            imgType.visible = true
            if tonumber(data["type"]) == 0 then
                data["type"] = 1;
            end
            imgType.url = "ui://ICONAtlas/" .. EnumConst.HeroTypeIcon[data["type"]]
        else
            if data.partnerId then
                local hero = logicMgr.HeroManager.getHeroDatabyModelId(tostring(data.partnerModelId),true)
                imgType.visible = true
                if hero then
                    imgType.url = "ui://ICONAtlas/" .. EnumConst.HeroTypeIcon[hero.type]
                end
            end
        end

        if data["level"] ~= nil and data["level"] > 0 then
            txtLv.visible = true
        end
        local lv, lvType = logicMgr.HeroManager.getHeroShowLevelByPartnerId(data.partnerId);
        if lv == 0 then
            lv = data["level"];
        end
        txtLv.text = string.TextColor(string.format(getLanguage("Level"), lv),  logicMgr.HeroManager.lvColors[lvType]);
        if isHeroSwap then
            txtLv.text = string.format(getLanguage("Level"), data["level"]);
        end
        if data["active"] ~= nil and data["active"] == 0 then
            objUnActive.selectedIndex = 0
        end
    else
        imgQualityBg.visible = true
        gemstoneController.selectedIndex = 0
        objUnActive.selectedIndex = 0
        showOwnCount.selectedIndex = 0
        objUnActiveSummonGroup = 0
        imgQualityBg.url = "ui://vktp164xdarwab"
    end
    if data then
        if data["isNewHero"] and this.isNewController then
            this.new_effect.alpha = 0;
            this.isNewController.selectedIndex = 1;
            data.isNewHero = nil;
        end
    end
    if type == EnumConst.FightTypeConst.RankBattle then
        local ctrl = obj:GetController("HotHero")
        local rate = obj:GetChild("n82")
        local name = obj:GetChild("Text_HeroName")
        rate.value = data.rate
        name.text = data.name
        if data.index > 4 then
            ctrl.selectedIndex = 4
        else
            ctrl.selectedIndex = data.index
        end
    end
    if iswish then
        objUnActive.selectedIndex = 2
    end
end
function HeadRectBItem.ShowOwnCount(obj,isShow)
    local ctrl = obj:GetController("showOwnCount")
    ctrl.selectedIndex = isShow and 1 or 0
end
function HeadRectBItem.ShowTypeAndLevel(obj, noShow)
    local ctrl = obj:GetController("ctrlUnActive")
    if noShow then
        ctrl.selectedIndex = 3
    end
end
function HeadRectBItem.ShowGuildName(obj, name)
    local ctrl = obj:GetController("Guild")
    if name then
        ctrl.selectedIndex = 1
        obj:GetChild("Text_PlayerName").text = name;
    else
        ctrl.selectedIndex = 0
    end
end

return HeadRectBItem;

